class Solution {
private:
  int sum = 0;
public:
  TreeNode* convertBST(TreeNode* root) {

    reverseInOrder(root);

    return root;

  }

  void reverseInOrder(TreeNode *root){

    if(root == nullptr){
      return;
    }

    reverseInOrder(root->right);
    root->val += sum;
    sum = root->val;
    reverseInOrder(root->left);

  }
};